/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.util.analyze;

import java.text.NumberFormat;

import soccerscope.model.ClientScene;
import soccerscope.model.GameEvent;

public class BallPossessionAnalyzer extends PassAnalyzer {

	public static String NAME = "Ball Possession";

	private int totalTime;

	@Override
	public void init() {
		super.init();
		this.totalTime = 0;
	}

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public Object getValueAt(int col, int fromTime, int toTime) {
		count(fromTime, toTime);
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumIntegerDigits(2);
		nf.setMaximumFractionDigits(2);
		switch (col) {
		case ROW_NAME:
			return getName();
		case LEFT:
			if (this.totalTime == 0)
				return "0";
			return nf.format(this.lcount * 100.0 / this.totalTime);
		case RIGHT:
			if (this.totalTime == 0)
				return "0";
			return nf.format(this.rcount * 100.0 / this.totalTime);
		default:
			return " ";
		}
	}

	@Override
	public void count(int fromTime, int toTime) {
		this.totalTime = 0;
		this.lcount = 0;
		this.rcount = 0;
		int pTable[] = PassAnalyzer.getPossessionTable();
		for (int i = fromTime; i <= toTime; i++) {
			if (i >= pTable.length)
				continue;
			switch (pTable[i]) {
			case PLAY_OFF:
				break;
			case PLAY_ON:
				this.totalTime++;
				break;
			case LEFT_SIDE:
				this.totalTime++;
				this.lcount++;
				break;
			case RIGHT_SIDE:
				this.totalTime++;
				this.rcount++;
				break;
			}
		}
	}

	@Override
	public GameEvent analyze(ClientScene clientScene, ClientScene prev) {
		return null;
	}
}
